package controllers

import config.{MyAwait, MyFile}
import dao._
import javax.inject.Inject
import models.Tables._
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}

class InsertController @Inject()(genomeDao: GenomeDao,

                                 cc: ControllerComponents) extends AbstractController(cc) with MyFile with MyAwait {

  def insert(insert: String) = Action {
    insert match {
      case "insertGenome" => insertGenome
    }
    Ok(Json.toJson("success"))
  }

  def insertGenome = {
    val path = "D:\\上海农科院大麦基因组\\annotation/Hordeum_vulgare_TRITEX_"
    val l = "D:\\上海农科院大麦基因组\\Hordeum_vulgare_tritex/Hordeum_vulgare_tritex.Morex_V2_scaf.51.gff3".readLines.filter(!_.startsWith("#")).map(_.split("\t")).filter(_ (2) == "gene")
    val pfamMap = s"${path}Pfam.txt".readLines.map(_.split("\t")).map(x => x.head -> x.last).toMap
    val goMap = s"${path}GOS.txt".readLines.map(_.split("\t")).map(x => x.head -> x.last).toMap
    val interproMap = s"${path}Interpro.txt".readLines.map(_.split("\t")).map(x => x.head -> x.last).toMap
    val patherMap = s"${path}PantherID.txt".readLines.map(_.split("\t")).map(x => x.head -> x.last).toMap

    val buffer = l.map { x =>
      val gene = x.last.split(";").head.drop(8)
      val chr = x.head
      val start = x(3).toInt
      val end = x(4).toInt
      val stand = x(6)
      val pfam = pfamMap.getOrElse(gene, "")
      val go = goMap.getOrElse(gene, "")
      val interpro = interproMap.getOrElse(gene, "")
      val pather = patherMap.getOrElse(gene, "")
      GenomeRow(0, gene,"Hordeum_vulgare_tritex", chr, start, end, stand, pfam, go, interpro, pather)
    }
    genomeDao.insert(buffer.toSeq).toAwait
  }


}
